package com.augurit.wf.common.web;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.jbpm.api.JbpmException;
import org.springframework.beans.factory.annotation.Autowired;

import com.augurit.common.base.action.ExtCrudActionSupport;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwCsdlwjService;
import com.augurit.gzcgwsp.smartformCsdlwj.service.IYwDlzyService;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.CsdlwjForm;
import com.augurit.gzcgwsp.smartformCsdlwj.web.form.DlzyForm;
import com.augurit.gzcgwsp.wfex.common.service.IWfServiceEX;
import com.augurit.wf.bi.web.form.WfActElemPrivDefForm;
import com.augurit.wf.bi.web.form.WfBusInstanceTaskForm;
import com.augurit.wf.common.entity.Jbpm4HistTask;
import com.augurit.wf.common.form.TraceRecord;
import com.augurit.wf.common.form.WfBusBaseEntity;
import com.augurit.wf.common.service.IWfBusClientService;
import com.augurit.wf.common.service.IWfService;
import com.augurit.wf.common.tag.opinion.WfOpinionTag;
import com.augurit.wf.common.util.WfUtils;

public abstract class WfBusClientExtAction<T extends WfBusBaseEntity> extends ExtCrudActionSupport<T> implements IWfBusClientAction<T> {

	private String templateCode;	//流程业务模板编号
	private Long taskInstDbid;		//任务实例主键
	private String procInstId;		//流程实例ID
	private String viewName;		//视图
	@Autowired
	private IYwCsdlwjService ywCsdlwjService;
	@Autowired
	private IYwDlzyService ywDlzyService;
	@Autowired
	private IWfService wfService;
	@Autowired
    private IWfServiceEX wfServiceEX;
	
	public abstract String wfBusSave() throws Exception;


	/**
     *获取某个环节的意见
     * @param activeName
     * @return
     */
    public  String getOpinion(String activeName){
    	List<Jbpm4HistTask> list = wfServiceEX.getHandleCommentsByTaskInstDbidAndActivityNames(getTaskInstDbid(),activeName);
		StringBuffer sb = new StringBuffer();
		if(list!=null && list.size()>0){
		for(int i=list.size()-1;i>=0;i--){//只取一条记录
			//if(StringUtils.isNotBlank(list.get(i).getHandleComments())){
			sb.append(list.get(i).getHandleComments());
			 break;
			//}
		}
		}
		return sb.toString().replace("null", "");
    }
	/**
	 * 加载流程历史意见列表
	 */
	public void initWfOpinions(){
		if(viewName == null){
			throw new JbpmException("视图参数不能为空，无法正确加载历史意见列表！");
		}
		
		Map<String, List<TraceRecord>> map = null;
		if(viewName.equals(WfActElemPrivDefForm.DB_ZB_VIEW)){
			map = this.loadWfOpinionsByTaskInstDbid();
		}
		else{
			map = this.loadWfOpinionsByProcInstDbid();
		}
		
		//城管委修改   开始
		if(map != null){
			Iterator it=map.entrySet().iterator();
            while(it.hasNext()){
            	Map.Entry entrys=(Map.Entry)it.next();
         	   	String key=(String) entrys.getKey();
         	   	List<TraceRecord>  list=new ArrayList<TraceRecord>();
         	   	List<TraceRecord> traceRecords = (List<TraceRecord>) entrys.getValue();
         	   	if(traceRecords.size()>0){//只取一条记录
         	   		TraceRecord record=null;
         	   		for(int k=traceRecords.size()-1;k>=0;k--){
         				record=traceRecords.get(k);
         				if(StringUtils.isNotBlank(record.getHandleComments())){
         					list.add(record);
         					map.put(key,  list);
         					break;
         				}
         	   		}
         	   }else{
         		   map.put(key,  traceRecords);
         	   }
            }
		}
		if("csdlwjqx".equals(this.getTemplateCode())){
			String id=getRequest().getParameter("id");
			setDlwjOpnion(templateCode, id, map);
		}
		//道路占用
		if("csdllszyspqx".equals(this.getTemplateCode())){
			String id=getRequest().getParameter("id");
			setDlwjOpnion(templateCode, id, map);
		}
		//城管委修改   结束
		getRequest().setAttribute(WfOpinionTag.WF_OPINIONS, map);
	}
	//区的道路挖掘
	private void setDlwjOpnion(String templateCode,String id,Map<String, List<TraceRecord>> map ){
		if(StringUtils.isNotBlank(id)){
			
			List<TraceRecord>  list=new ArrayList<TraceRecord>();
			List<TraceRecord>  list1=new ArrayList<TraceRecord>();
			List<TraceRecord>  list2=new ArrayList<TraceRecord>();
			TraceRecord record=new TraceRecord();
			TraceRecord record1=new TraceRecord();
			TraceRecord record2=new TraceRecord();
			CsdlwjForm form=ywCsdlwjService.get(Long.valueOf(id));
			if(form!=null && StringUtils.isNotBlank(form.getCsryj())){
				record.setHandleComments(form.getCsryj());
				record.setAssignee(form.getCsr());
				record.setHandleCommentsTime(form.getCssj());
				record1.setHandleComments(form.getFsyj());
				record1.setAssignee(form.getFsr());
				record1.setHandleCommentsTime(form.getFssj());
				record2.setHandleComments(form.getScryj());
				record2.setAssignee(form.getScr());
				record2.setHandleCommentsTime(form.getScsj());
				list.add(record);
				list1.add(record1);
				list2.add(record2);
				map.put("jbrcs", list);
				map.put("czgldsh", list);
				map.put("wfgldsh", list);
			}
		
		}
	}
	
	//道路占用
	private void setDlzyOpnion(String templateCode,String id,Map<String, List<TraceRecord>> map ){
		if(StringUtils.isNotBlank(id)){
			List<TraceRecord>  list=new ArrayList<TraceRecord>();
			List<TraceRecord>  list1=new ArrayList<TraceRecord>();
			List<TraceRecord>  list2=new ArrayList<TraceRecord>();
			TraceRecord record=new TraceRecord();
			TraceRecord record1=new TraceRecord();
			TraceRecord record2=new TraceRecord();
			DlzyForm form=ywDlzyService.get(Long.valueOf(id));
			if(form!=null && StringUtils.isNotBlank(form.getCsryj())){
				record.setHandleComments(form.getCsryj());
				record.setAssignee(form.getCsr());
				record.setHandleCommentsTime(form.getCssj());
				record1.setHandleComments(form.getFsyj());
				record1.setAssignee(form.getFsr());
				record1.setHandleCommentsTime(form.getFssj());
				record2.setHandleComments(form.getScryj());
				record2.setAssignee(form.getScr());
				record2.setHandleCommentsTime(form.getScsj());
				list.add(record);
				list1.add(record1);
				list2.add(record2);
				map.put("jbrcs", list);
				map.put("czgldsh", list);
				map.put("wfgldsh", list);
			}
		
		}
	}
	
	
	public WfBusInstanceTaskForm wfBusSave(IWfBusClientService service, T form, String masterEntity){
		WfBusInstanceTaskForm task = service.wfBusSave(form, this.isNewForm(form), masterEntity,
				this.getTemplateCode(),	this.getTaskInstDbid(), this.getLoginUserName());
		return task;
	}
	
	/**
	 * 主动判断是否新对象,不用id判断。何生添加
	 * @param service
	 * @param form
	 * @param bool
	 * @param masterEntity
	 * @return
	 */
	public WfBusInstanceTaskForm wfBusSave(IWfBusClientService service, T form, Boolean bool ,String masterEntity){
		WfBusInstanceTaskForm task = service.wfBusSave(form, bool, masterEntity,
				this.getTemplateCode(),	this.getTaskInstDbid(), this.getLoginUserName());
		return task;
	}
	
	public Map<String, List<TraceRecord>> loadWfOpinionsByTaskInstDbid(){
		if(taskInstDbid != null){
	 	    List<TraceRecord> list = wfService.getTraceInfoOrderByActName(taskInstDbid);
	 	    return WfUtils.constructOpinionsMap(list);
	    }
		return null;
	}
	
	public Map<String, List<TraceRecord>> loadWfOpinionsByProcInstDbid(){
		if(procInstId != null && procInstId.trim().length() > 0){
	 	    List<TraceRecord> list = wfService.getTraceInfo(procInstId);
	 	    return WfUtils.constructOpinionsMap(list);
	    }
		return null;
	}
	
	public boolean isNewForm(final T form) {
		return form.getId() == null ? true : false;
	}
	
	public String getTemplateCode() {
		return templateCode;
	}
	
	public Long getTaskInstDbid() {
		return taskInstDbid;
	}	
	
	//属性setter方法
	public void setTemplateCode(String templateCode) {
		this.templateCode = templateCode;
	}

	public void setTaskInstDbid(Long taskInstDbid) {
		this.taskInstDbid = taskInstDbid;
	}

	public String getViewName() {
		return viewName;
	}

	public void setViewName(String viewName) {
		this.viewName = viewName;
	}
	
	public String getProcInstId() {
		return procInstId;
	}

	public void setProcInstId(String procInstId) {
		this.procInstId = procInstId;
	}
}
